package 树;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //先new一颗二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的节点
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode wy = new HeroNode(2, "吴用");
        HeroNode ljy = new HeroNode(3, "卢俊义");
        HeroNode lc = new HeroNode(4, "林冲");

        //手动创建树
        root.setLeft(wy);
        root.setRight(ljy);
        ljy.setRight(lc);
        binaryTree.setRootNode(root);

        //测试查找
        HeroNode heroNode = binaryTree.postOrdersearch(2);
        if (heroNode != null) {
            System.out.println(heroNode);
        } else {
            System.out.println("未找到结点！");
        }

    }
}

//定义一颗二叉树
class BinaryTree {
    private HeroNode rootNode;

    public void setRootNode(HeroNode rootNode) {
        this.rootNode = rootNode;
    }

    //前序遍历
    public void preOrder() {
        if (this.rootNode != null) {
            this.rootNode.preOrder();
        } else {
            System.out.println("当前二叉树为空");
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.rootNode != null) {
            this.rootNode.infixOrder();
        } else {
            System.out.println("当前二叉树为空");
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.rootNode != null) {
            this.rootNode.postOrder();
        } else {
            System.out.println("当前二叉树为空");
        }
    }

    //前序遍历查找
    public HeroNode preOrdersearch(int no) {
        if (rootNode != null) {
            return rootNode.preOrdersearch(no);
        } else {
            return null;
        }

    }

    //中序遍历查找
    public HeroNode infixOrdersearch(int no) {
        if (rootNode != null) {
            return rootNode.infixOrdersearch(no);
        } else {
            return null;
        }
    }

    //后序遍历查找
    public HeroNode postOrdersearch(int no) {
        if (rootNode != null) {
            return rootNode.postOrdersearch(no);
        } else {
            return null;
        }
    }


}

//先创建HeroNode节点
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        super();
        this.no = no;
        this.name = name;

    }

    public int getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //编写前序遍历方法
    public void preOrder() {
        System.out.println(this);   //先输出父节点
        //递归向左子树
        if (this.left != null) {
            this.left.preOrder();
        }
        //递归向右子树
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    //中序遍历
    public void infixOrder() {
        //递归向左子树
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    //后序遍历
    public void postOrder() {
        //递归向左子树
        if (this.left != null) {
            this.left.infixOrder();
        }
        if (this.right != null) {
            this.right.infixOrder();
        }
        System.out.println(this);
    }

    //前序遍历查找
    public HeroNode preOrdersearch(int no) {
        int sum = 0;
        HeroNode resNode = null;
        //比较当前结点
        if (this.no == no) {
            resNode = this;
        }
        //判断左子节点是否为空，如果不为空，则递归前序查找
        //如果找到节点则返回
        if (this.left != null) {
            resNode = this.left.preOrdersearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preOrdersearch(no);
        }
        sum = sum + 1;
        System.out.println(sum);
        return resNode;

    }

    //中序遍历
    public HeroNode infixOrdersearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrdersearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no) {
            resNode = this;
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.infixOrdersearch(no);
        }
        return resNode;
    }

    //后序遍历
    public HeroNode postOrdersearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrdersearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.infixOrdersearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no) {
            resNode = this;
            return resNode;
        }
        return resNode;
    }
}
